package org.dydl.dao.system.repository;

import java.util.List;

import org.dydl.model.system.OrderDetail;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;


@Repository
public interface IOrderDetailRepository extends CrudRepository<OrderDetail, Integer>,JpaSpecificationExecutor<OrderDetail>{
	
	
	@Query(value=" FROM OrderDetail ord  WHERE ord.orderNo LIKE :orderNo",countQuery="SELECT count(ord.id) FROM OrderDetail ord WHERE ord.orderNo LIKE :orderNo")
	Page<OrderDetail> getOrderdetailListLike(@Param("orderNo")String orderNo,Pageable pageable);
	
	@Query(value = "SELECT * FROM orders_detail ord WHERE ord.ORDER_NO = :orderNo",nativeQuery = true)
	List<OrderDetail> getOrderDetailList(@Param("orderNo")String orderNo);
	
	@Query(value = "SELECT * FROM orders_detail WHERE ORDER_NO = :orderNo" , nativeQuery=true)
	List<OrderDetail> findByOrderNo(@Param("orderNo")String orderNo);
	
	@Modifying
	@Query(value = "DELETE FROM orders_detail WHERE ORDER_NO= :orderNo" , nativeQuery = true)
	int deleteDetail(@Param("orderNo")String orderNo);
	
	@Modifying
	@Query(value = "UPDATE orders_detail SET ALREADY = ALREADY + :already WHERE GOODS_UUID = :goodsUuid" , nativeQuery = true)
	void updateAlreadyByGoodsUuid(@Param("goodsUuid")String goodsUuid ,@Param("already")Integer already);
	
	@Modifying
	@Query(value = "UPDATE orders_detail SET ALREADY_OUT = ALREADY_OUT + :alreadyOut ,ALREADY = ALREADY - :alreadyOut WHERE UUID = :orderDetailUuid" , nativeQuery = true)
	void updateAlreadyOutByOrderDetailUuid(@Param("orderDetailUuid")String orderDetailUuid ,@Param("alreadyOut")Integer alreadyOut);
	
	@Modifying
	@Query(value = "UPDATE orders_detail SET ALREADY = ALREADY + :already WHERE UUID = :orderDetailUuid" , nativeQuery = true)
	void updateAlreadyByOrderDetailUuid(@Param("orderDetailUuid")String orderDetailUuid,@Param("already")Integer already);

}
